#!/bin/bash

DIR=`dirname "$0"`
WWWDIR=~/www
BADDIR=~/bad
WEBSITE=$DIR/../website
HTACCESS=$DIR/htaccess_fixes

MOVEBAD=0

HOST=$(hostname)

if [ ! $HOST == "cubist.cs.washington.edu" ]; then
    echo ">>> Sorry, this doesn't appear to be cubist. Please execute this script ONLY from cubist"
    exit 1
fi


read -p ">>> This script will delete your ~/www directory. Is that okay? [y/n]: " CHOICE

if [ ! $CHOICE == "y" -a ! $CHOICE == "yes" ]; then
    exit 0
fi

echo ">>> Deleting www"

# Avoid hassle checking if it doesn't exist
mkdir $WWWDIR

# Told you so...
rm -rf $WWWDIR 2> /dev/null

# Check return status of the rm
if [ $? -ne 0 ]; then
    echo ">>> Can't delete your www directory. Going to attempt a fun workaround."

    if [ -d $BADDIR -o -f $BADDIR ]; then
        echo ">>> Sorry, please remove the $BADDIR or $WWWDIR directory and rerun."
        exit 1
    fi
    
    mv $WWWDIR $BADDIR
    MOVEBAD=1
fi

echo ">>> Copying website into $WWWDIR"
cp -r $WEBSITE $WWWDIR

echo ">>> Copying htaccess fixes (This is why cubist isn't fun :*(   )"
sed 's_USERNAME_~'$USER'_' $HTACCESS/.htaccesswww > $WWWDIR/.htaccess
sed 's_USERNAME_~'$USER'_' $HTACCESS/.htaccesswebroot > $WWWDIR/app/webroot/.htaccess


read -p ">>> Would you like to use our database? Yes if so, no if you want to use your own. If you use your own, the build_config file must be completely filled out and the databases created. [y/n] " DB

if [ $DB == "y" ]; then
	echo ">>> Copying sfp info (to set up database connections)"
	cp /www/htdocs/projects/11wi/cse403/SimpleFamilyPhotos/sfp_info.php $WWWDIR/../sfp_info.php
else
	CONFIG="$DIR""/build_config"
	if [ ! -f $CONFIG ]; then
		echo "Touching config file"
		touch "$CONFIG""/build_config"
	fi

	# Read all the config values into respective variables
	while read line; do
		# Skip comments
		if [[ ${line:0:1} == "#" ]]; then
			continue
		fi
	
		CONFIG_ITEM=`echo "$line" | grep "="`
	
		if [[ ! "$CONFIG_ITEM" == "" ]]; then
			# Parse variable name and value from config item form name=value
			VAR_NAME=`echo "$CONFIG_ITEM" | sed 's_^\(.*\)=.*_\1_'`
			VAR_VALUE=`echo "$CONFIG_ITEM" | sed 's_^.*=\(.*\)_\1_'` 
			
			if [[ ! "$VAR_VALUE" == "" ]]; then
			    eval "$VAR_NAME"="$VAR_VALUE"
			fi
		fi
	done < "$CONFIG"
	
	## Create cubist_db_info.php
	echo "<?php" > $WWWDIR/../sfp_info.php
	echo "define('cse403_user', "\'$MYSQL_USER\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_pass', "\'$MYSQL_PASS\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_salt', "\'$CAKE_SALT\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_seed', "\'$CAKE_SEED\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_dbloc', "\'$MYSQL_LOCATION\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_dbname', "\'$MYSQL_DB\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_dbtestname', "\'$MYSQL_DB"_test"\'");" >> $WWWDIR/../sfp_info.php
	echo "define('cse403_dbtest2name', "\'$MYSQL_DB"_test2"\'");" >> $WWWDIR/../sfp_info.php

	echo ">>> Creating the dbs"
	echo "drop database $MYSQL_DB" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS"
	echo "create database "$MYSQL_DB"_test" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS"
	echo "create database "$MYSQL_DB"_test2" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS"
	echo "create database $MYSQL_DB" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS"
	echo "source $DIR/create_tables.sql" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS" --database="$MYSQL_DB"
	echo "source $DIR/insert_test_values.sql" | mysql --host="$MYSQL_LOCATION" --user="$MYSQL_USER" --password="$MYSQL_PASS" --database="$MYSQL_DB"	

fi


echo ">>> Doing the usual permissions stuff for CakePHP"
chmod -R 777 $WWWDIR/app/tmp
chmod -R 777 $WWWDIR/app/webroot/pix

if [[ $MOVEBAD -ne 0 ]]; then
    echo ">>> Applying workaround file move"
    mv $BADDIR $WWWDIR/
fi 
